﻿using EyFramework.Common;
using SqlSugar;
using System;
using System.Collections.Generic;

namespace EyFramework.ORM
{
    public class OrmConstants
    {
        /// <summary>
        /// 保存数据库连接的配置，不要每次都去创建
        /// </summary>
        public static Dictionary<string, ConnectionConfig> DbConfigs = new Dictionary<string, ConnectionConfig>();

        private static string key = "xsey1957XSEY1957";

        public static ConnectionConfig CreateConnectionConfig(string connKey)
        {
            ConnectionConfig config = null;
            if (Globals.appConfigure.CurrentConfig.ContainsKey(connKey.ToUpper()))
            {
                Dictionary<string, string> dbConfig = Globals.appConfigure.CurrentConfig[connKey.ToUpper()];
                string dbType = dbConfig.ContainsKey("TYPE") ? dbConfig["TYPE"] : "unconfigure";
                switch (dbType)
                {
                    case Constants.DATABASE_ORACLE:
                        config = CreateOracleConfig(dbConfig); break;
                    case Constants.DATABASE_MYSQL:
                        config = CreateMysqlConfig(dbConfig); break;
                    case Constants.DATABASE_SQLSERVER:
                        config = CreateSqlserverConfig(dbConfig); break;
                    case Constants.DATABASE_SQLITE:
                        config = CreateSqliteConfig(dbConfig); break;
                    default:
                        throw new BizException(string.Format("数据库类型[{0}]暂不支持，请使用其他数据库.", dbType));
                }
            }
            else
            {
                throw new BizException(string.Format("application.ini文件中未找到Section为{0}的数据库配置.", connKey.ToUpper()));
            }

            return config;
        }

        /// <summary>
        /// 创建Sqlite的数据库配置
        /// </summary>
        /// <param name="dbConfig"></param>
        /// <returns></returns>
        private static ConnectionConfig CreateSqliteConfig(Dictionary<string, string> dbConfig)
        {
            throw new NotImplementedException();
        }

        private static ConnectionConfig CreateSqlserverConfig(Dictionary<string, string> dbConfig)
        {
            string ConnectionString = "Data Source={0};Database={1};User ID={2};Password={3}";
            ConnectionString = string.Format(ConnectionString, dbConfig["HOST"], dbConfig["DATABASE"], AESHelper.AESDecrypt(dbConfig["USERID"], key), AESHelper.AESDecrypt(dbConfig["PASSWORD"], key));
            return new ConnectionConfig()
            {
                ConnectionString = ConnectionString,
                DbType = DbType.SqlServer,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
            };
        }

        private static ConnectionConfig CreateMysqlConfig(Dictionary<string, string> dbConfig)
        {
            throw new NotImplementedException();
        }

        private static ConnectionConfig CreateOracleConfig(Dictionary<string, string> dbConfig)
        {
            string ConnectionString =
                "User Id={0};Password={1};Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={2})(PORT={3})))(CONNECT_DATA=(SERVICE_NAME={4})))";
            if (!bool.Parse(dbConfig["BALNCE"]))
            {
                ConnectionString = string.Format(ConnectionString,
                    AESHelper.AESDecrypt(dbConfig["USERID"], key),
                    AESHelper.AESDecrypt(dbConfig["PASSWORD"], key),
                    dbConfig["HOST"],
                    dbConfig["PORT"],
                    dbConfig["SID"]);
            }
            return new ConnectionConfig()
            {
                ConnectionString = ConnectionString,
                DbType = DbType.Oracle,
                InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
                IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样我就不多解释了
            };
        }
    }
}